﻿require.config({
	urlArgs: "version=20170407",
    paths: {
        'jquery': 'packages/jquery',
        'boottree': 'packages/selfadmin-treeview',
        'bootstrap': '../bootstrap/js/bootstrap.min'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'bootstrap'
        },
    	'boottree':{
    		deps:['jquery'],
    		exports: 'boottree'
    	}
    }
});

require(['jquery', 'boottree','bootstrap'],function($){
	var perms_list={};
	var account_id=0;
	var account_name="";
	var treeshow_bool = false;
	$.ajax({
		type : 'POST',
		url : '/users/getId',
		//data : jsonda,
	    cache: false,
		async:false,
		//contentType: "application/json; charset=utf-8",
		dataType : 'json',
		success : function(res) {
			if (res.status == 200) {
					account_id=res.data.id;
					account_name=res.data.name;
					document.getElementById("user-info").innerHTML="&nbsp"+res.data.name;
			} else
				alert("加载失败！");
		},
		error : function(xml, err, throwInfo) {
			alert(err);
		}
    });
	$.ajax({
		type : 'POST',
		url : '/users/getPermList',
		//data : jsonda,
	    cache: false,
		async:false,
		//contentType: "application/json; charset=utf-8",
		dataType : 'json',
		success : function(res) {
			if (res.status == 200) {
				perms_list=res.entries;
			} else
				alert("加载失败！");
		},
		error : function(xml, err, throwInfo) {
			alert(err);
		}
    });
	getTree();
	function getTree(){
		$('#treeview5').innerHTML="";
		var defaultData = [];
		if(!!perms_list){
			for(var i in perms_list){
				var perm_list = perms_list[i];
				if(perm_list.Ename=="admin：selfplib"){
					defaultData.push({
	                	 id:'filelist-h0',
	                	 text: '我的方案',
	                   });
				}
				if(perm_list.Ename=="admin：selfmlib"){
					defaultData.push({
                   		id:'filelist-f0',
		            	text: '我的素材',
	                 });
				}
				if(perm_list.Ename=="admin：mlib"){
					defaultData.push({
                   		id:'filelist-s0',
		            	text: '素材库',
		            	
	                 });
				}
				if(perm_list.Ename=="admin：plib"){
					defaultData.push({
		            	 id:'filelist-t0',
		            	 text: '案例库',
	                 });
				}
				if(perm_list.Ename=="admin：useradmin"){
					defaultData.push({
		                 id:'filelist-e0',
		                 text: '用户管理',
		             });
				}
				if(perm_list.Ename=="admin：roleadmin"){
					defaultData.push({
				         id:'filelist-n0',
				         text: '角色管理',
				    });
				}
			}
			defaultData.push({
			      id:'filelist-0',
			      text: '个人中心',
			 });
		}
		$('#treeview5').treeview({
			 color: "#41464B",
	         selectedBackColor:"#41464B",
	         expandIcon: 'glyphicon glyphicon-chevron-right',
	         collapseIcon: 'glyphicon glyphicon-chevron-down',
	         nodeIcon: 'glyphicon',
	         data:  defaultData
	    });				
	}
	
	$('#user-info').mouseover(function(){
		document.getElementById("user-info").innerHTML="&nbsp"+"退出登录";
	});
	$('#user-info').mouseleave(function(){
		document.getElementById("user-info").innerHTML="&nbsp"+account_name;
	});
	$('#show-tree').click(function(event){
		var hidebg=document.getElementById('hidebg');
    	if(!treeshow_bool){
    		$('#left-bar')[0].style.display="block";
    		treeshow_bool=true;
    	  	hidebg.style.display="block";
    	}else{
    		$('#left-bar')[0].style.display="";
    		treeshow_bool=false;
    		hidebg.style.display="none";
    	}
    });
	$('#hidebg').click(function(event){
		$('#left-bar')[0].style.display="";
		treeshow_bool=false;
		hidebg.style.display="none";
	});
	$('#user-logout').click(function(){
		$.ajax({
			type : 'GET',
			url : '/logout',
			//data : jsonda,
		    cache: false,
			async:false,
			//contentType: "application/json; charset=utf-8",
			dataType : 'json',
			success : function(res) {
				if (res.status == 200) {
						window.location.href="/login.html";
				} else
					alert("退出登录失败！");
			},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
	    });	
	});
	setUserInfo();
	$('#password-submit').click(function(){
		$.ajax({
			type : 'PUT',
			url : '/users/editPassword',
			async:false,
			data : {
				"old_password":$('#old-password').val(),
				"new_password":$('#new-password').val()
				},
			//timeout:7000,//请求超时后停止请求
			dataType : 'json',
			success : function(res) {
			if (res.status == 200) {
				alert("提交成功!!");
			}else if (res.status == 403) {
				alert("密码错误!!");
			}else{
				alert("加载失败");
			}},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
		});
	});
	$('#save-userInfo').click(function(){
		//var com_name=$('#com_name').val();
		//var com_id=$('#com_id').val();
		var user_name=$('#user_name').val();
		//var tele_num=$('#tele_num').val();
		 $.ajax({
			type : 'PUT',
			url : '/users/editUserName',
			async:false,
			data : {
				//"com_name":com_name,
				//"com_id":com_id,
				"user_name":user_name,
				//"tele_num":tele_num,
				},
			//timeout:7000,//请求超时后停止请求
			dataType : 'json',
			success : function(res) {
			if (res.status == 200) {
				alert("提交成功!!");
				if(!!com_name){
					$('#com_name').attr("value",com_name);
				}else{
					$('#com_name').attr("placeholder","未设置");
				}
				
				if(!!com_id){
					$('#com_id').attr("value",com_id);
				}else{
					$('#com_id').attr("placeholder","未设置");
				}
				if(!!user_name){
					$('#user_name').attr("value",user_name);
				}else{
					$('#user_name').attr("placeholder","未设置");
				}
				if(!!tele_num){
					$('#tele_num').attr("value",tele_num);
				}else{
					$('#tele_num').attr("placeholder","未设置");
				}
			}else{
				alert("加载失败");
			}},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
		});
	});
	
	function setUserInfo(){
		$.ajax({
			type : 'POST',
			url : '/users/getUserInfo',
			//data : jsonda,
		    cache: false,
			async:false,
			//contentType: "application/json; charset=utf-8",
			dataType : 'json',
			success : function(res) {
				if (res.status == 200) {
					var com_name=res.data.com_name;
					var com_id=res.data.com_id;
					var user_id=res.data.id;
					var user_name=res.data.name;
					var tele_num=res.data.tele_num;
					
					if(!!com_name){
						$('#com_name').attr("value",com_name);
					}else{
						$('#com_name').attr("placeholder","未设置");
					}
					
					if(!!com_id){
						$('#com_id').attr("value",com_id);
					}else{
						$('#com_id').attr("placeholder","未设置");
					}
					
					if(!!user_id){
						$('#user_id').attr("value",user_id);
					}else{
						$('#user_id').attr("placeholder","未设置");
					}
					
					if(!!user_name){
						$('#user_name').attr("value",user_name);
					}else{
						$('#user_name').attr("placeholder","未设置");
					}
					if(!!tele_num){
						$('#tele_num').attr("value",tele_num);
					}else{
						$('#tele_num').attr("placeholder","未设置");
					}
				} else
					alert("加载失败！");
			},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
	    });
	}  
});
